package com.xianyu.controller;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;


@Controller
public class CodeController {

    @PostMapping("/msg")
    public String SendMsg( String tel, HttpServletRequest request) {
        String codes = "0123456789";
        Random random = new Random();
        StringBuilder sms = null;
        StringBuilder randomStr = new StringBuilder();
        for (int i = 0; i < 6; i++) {
            sms = randomStr.append(codes.charAt(random.nextInt(10)));

        }

       /* System.out.println(sms); //查看生成的sms是什么
        System.out.println(tel);
        System.out.println("s==============");*/
        /*request.getSession().remove("s");*/
        //将验证码存入到session中
        request.getSession().setAttribute("sms", sms);

        try {
            Credential cred = new Credential("AKIDhFclfMq57yd5JJ4bv40jFZeXCcoqhLkx", "4uVwBCyWptiKl01oOkrjOp20jHgttiLl");

            HttpProfile httpProfile = new HttpProfile();
            //httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            SmsClient client = new SmsClient(cred, "ap-beijing", clientProfile);

            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {"86" + tel};
            req.setPhoneNumberSet(phoneNumberSet1);

            req.setSmsSdkAppId("1400534900");
            req.setSignName("PrunT");
            req.setTemplateId("1001489");



            /*req.setSmsSdkAppId("1400534900");
            req.setSignName("闲鱼");
            req.setTemplateId("1006720");*/


//                Sendcode sc = new Sendcode();
//                sc.setMsg(sms.toString());
//                sc.setTel(tel.toString());
//                System.out.println(sc);

//                System.out.println(sms);
            String[] templateParamSet = new String[]{String.valueOf(sms)};
            /*System.out.println(templateParamSet);*/

//            boolean s = templateParamSet[0].toString().equals(sms.toString());
//            System.out.println(s);
//

            req.setTemplateParamSet(templateParamSet);
            SendSmsResponse resp = client.SendSms(req);
            if ("ok".equalsIgnoreCase(resp.getSendStatusSet()[0].getCode())) {
                return "true";
            }
           /* System.out.println(SendSmsResponse.toJsonString(resp));*/


        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return "false";


    }

}
